/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.common.util.StringFilterUtil;
import com.eyaomall.wxmall.model.po.DiscountGoods;
import com.eyaomall.wxmall.model.vo.DiscountGoodsVO;
import com.eyaomall.wxmall.model.vo.DiscountRecordVO;
import com.eyaomall.wxmall.service.DiscountGoodsService;
import com.eyaomall.wxmall.service.ResourceService;
import com.eyaomall.wxmall.service.SmsService;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.cmp.kit.Pages;
import com.lycheeframework.core.exception.ErrorCodeException;

/**
 * 砍价功能
 * 
 * @author yuhuafeng
 *
 */
@Controller
@RequestMapping("/discount")
public class DiscountController {
	@Autowired
	private DiscountGoodsService discountGoodsService;

	@Autowired
	private ResourceService resourceService;
	@Autowired
	private SmsService smsService;

	/**
	 * 跳转至砍价列表页
	 * 
	 * @return
	 */
	@RequestMapping("index")
	public String index() {

		return "discount/index";
	}

	/**
	 * 查询商品砍价分页
	 * 
	 * @return
	 */
	@RequestMapping("page_data")
	public @ResponseBody Pages<?> page_data(PageHTTPObject httpObject) {
		DiscountGoods vo = new DiscountGoods();
		// 只显示上线的，并且在未结束的活动
		vo.setStatus("1");
		vo.setGmtEnd(new Date());
		vo.setShopId(MemberHelper.getShopId());
		return discountGoodsService.pageByCustom(vo, httpObject);
	}

	/**
	 * 跳转至用户砍价列表页
	 * 
	 * @return
	 */
	@RequestMapping("memberindex")
	public String memberindex() {

		return "discount/memberindex";
	}

	/**
	 * 查询商品砍价分页
	 * 
	 * @return
	 */
	@RequestMapping("member_page_data")
	public @ResponseBody Pages<?> member_page_data(PageHTTPObject httpObject) {
		if (MemberHelper.getId() == 0 || MemberHelper.getId() == null)
			throw new ErrorCodeException(BECode.global_900008);
		DiscountRecordVO vo = new DiscountRecordVO();
		vo.setMemberId(MemberHelper.getId());// 此处调用获取用户id接口
		return discountGoodsService.pageRecord(vo, httpObject);
	}

	/**
	 * 跳转至砍价详情页
	 * 
	 * @return
	 */
	@RequestMapping("details")
	public String details(Integer discountid, Map<String, Object> mode) {
		// 查询活动详情
		if (discountid != null)
			mode.put("discount", discountGoodsService.queryById(discountid));
		mode.put("rule", resourceService.getOption(118));
		return "discount/details";
	}

	/**
	 * 参加砍价活动
	 * 
	 * @param discountid
	 *            活动id
	 * @return
	 */
	@RequestMapping("join")
	public @ResponseBody GsnData join(Integer discountid, Integer num) {

		return GsnResponse.toData(discountGoodsService.join(discountid, 1));
	}

	/**
	 * 跳转至我的砍价活动详情页
	 * 
	 * @param discountid
	 *            用户砍价活动id
	 * 
	 * @return
	 */
	@RequestMapping("memberdetails")
	public String memberdetails(Integer discountid, Map<String, Object> mode) {
		// 查询活动详情
		if (discountid != null) {
			DiscountRecordVO vo = discountGoodsService.queryByMemberDiscountId(discountid);
			if (vo == null) {
				throw new ErrorCodeException(BECode.discount_6000001);
			}
			// 用户活动信息
			mode.put("record", vo);
			// 活动详情
			DiscountGoodsVO goods = discountGoodsService.queryById(vo.getDiscountId());
			if (goods == null) {
				throw new ErrorCodeException(BECode.discount_6000001);
			}
			mode.put("discount", goods);
			mode.put("shopId", MemberHelper.getShopId());
			// 帮忙砍价记录
			PageHTTPObject page = new PageHTTPObject();
			page.setPageNum(1);
			mode.put("log", discountGoodsService.queryListLog(vo.getId(), page));
			if ("0".equals(goods.getStatus()) || "1".equals(vo.getIssuccess()) || "0".equals(vo.getStatus())) {
				mode.put("countdowntime", -1);
			} else {
				mode.put("countdowntime", vo.getGmtEnd().getTime() - new Date().getTime());
			}
			if (vo.getMemberId() == MemberHelper.getId())// 判断是否是本人,此处需要获取用户信息
				mode.put("isMember", "y");
		}
		mode.put("rule", resourceService.getOption(118));
		return "discount/memberdetails";
	}

	/**
	 * 跳转至手机短信验证页面
	 * 
	 * @return
	 */
	@RequestMapping("toCode")
	public String toCode(Integer discountid, Map<String, Object> mode) {
		mode.put("discount", discountid);
		return "discount/code";
	}

	/**
	 * 获取帮忙砍价手机验证码
	 * 
	 * @param discountid
	 *            会员参与id
	 * @return
	 */
	@RequestMapping("code")
	public @ResponseBody GsnData code(String phone) {
		// 此处验证手机号，验证手机号格式
		if (!StringFilterUtil.PhoneFilter(phone))
			throw new ErrorCodeException(BECode.global_900000);
		String wxname = "药易方";
		smsService.bargain(phone, wxname);

		return GsnResponse.toData(true);
	}

	/**
	 * 砍价
	 * 
	 * @param discountid
	 *            会员参与id
	 * @return
	 */
	@RequestMapping("bargain")
	public @ResponseBody GsnData bargain(Integer discountid) {
		// 此处进行验证码验证，防止恶意手机号输入
		// smsService.checkSmsCode(phone, code, SmsConstants.BARGAIN_TYPE);

		return GsnResponse.toData(discountGoodsService.bargain(discountid, MemberHelper.getId() + ""));
	}

	/**
	 * 跳转至砍价成功页
	 * 
	 * @return
	 */
	@RequestMapping("success")
	public String success(Integer discountid, Map<String, Object> mode) {
		DiscountRecordVO vo = discountGoodsService.queryByMemberDiscountId(discountid);
		if (vo != null)
			mode.put("money", vo.getPrice());
		return "discount/success";
	}

	/**
	 * 获取砍价活动现价
	 * 
	 * @param discountid
	 *            会员参与id
	 * @return
	 */
	@RequestMapping("price")
	public @ResponseBody GsnData price(Integer discountid, Map<String, Object> mode) {
		DiscountRecordVO vo = discountGoodsService.queryByMemberDiscountId(discountid);
		if (vo == null)
			throw new ErrorCodeException(BECode.discount_6000001);
		return GsnResponse.toData(vo);
	}

}
